package com.hspedu.seckill.controller;

import com.hspedu.seckill.pojo.User;
import com.hspedu.seckill.service.GoodsService;
import com.hspedu.seckill.vo.GoodsVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ThymeleafViewResolver thymeleafViewResolver;


    /**
     * 响应请求商品列表
     */
    @RequestMapping(value = "/toList", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toList(Model model, User user, HttpServletRequest request, HttpServletResponse response) {
        if (null == user) {
            return "login";
        }

        //在redis获取html页面
        String html = (String) redisTemplate.opsForValue().get("goodsList");

        //如果redis有就返回
        if (StringUtils.hasText(html)) {
            return html;
        }


        List<GoodsVo> goodsList = goodsService.findGoodsVo();
        model.addAttribute("user", user);
        model.addAttribute("goodsList", goodsList);

        //如果redis没有就手动渲染页面
        WebContext webContext = new WebContext(request, response, request.getServletContext(), response.getLocale(), model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsList", webContext);

        //将html页面放入redis
        if (StringUtils.hasText(html)) {
            redisTemplate.opsForValue().set("goodsList", html, 30, TimeUnit.SECONDS);
        }
        //返回
        return html;
    }

    /**
     * 展示用户详情列表
     */
    @RequestMapping(value = "/toDetail/{goodsId}", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toDetail(Model model, @PathVariable Long goodsId, User user, HttpServletRequest request, HttpServletResponse response) {

        if (null == user) {
            return "login";
        }


        String html = (String) redisTemplate.opsForValue().get("goodsDetail:" + goodsId);

        if (StringUtils.hasText(html)) {
            return html;
        }

        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);

        //秒杀开始时间
        Date startDate = goodsVo.getStartDate();
        //秒杀结束时间
        Date endDate = goodsVo.getEndDate();
        //当前时间
        Date nowDate = new Date();

        //秒杀状态 0 秒杀倒计时 1 秒杀进行中 2 秒杀已结束
        int secKillStatus = 0;
        //秒杀开发还有多少秒
        int remainSeconds = 0;

        //秒杀未开始
        if (nowDate.before(startDate)) {
            remainSeconds = (int) ((nowDate.getTime() - startDate.getTime()) / 1000);
            //秒杀已结束
        } else if (nowDate.after(endDate)) {
            secKillStatus = 1;
            remainSeconds = -1;
        } else {
            secKillStatus = 2;
        }

        model.addAttribute("secKillStatus", secKillStatus);
        model.addAttribute("remainSeconds", remainSeconds);
        model.addAttribute("goods", goodsVo);
        model.addAttribute("user", user);

        WebContext webContext = new WebContext(request, response, request.getServletContext(), response.getLocale(), model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goodsDetail", webContext);

        if (StringUtils.hasText(html)) {
            redisTemplate.opsForValue().set("goodsDetail:" + goodsId, html, 30, TimeUnit.SECONDS);
        }
        return html;
    }

}
